package com.example.taobaounion.ui.activity

import android.content.*
import android.util.Log
import android.view.View
import android.widget.Toast
import com.bumptech.glide.Glide
import com.example.taobaounion.R
import com.example.taobaounion.base.BaseActivity
import com.example.taobaounion.model.bean.DiscountTicket
import com.example.taobaounion.presenter.impl.DiscountTicketPresenterImpl
import com.example.taobaounion.view.IDiscountTicketCallBack
import com.example.taobaounion.utils.CheackUtils
import com.example.taobaounion.utils.PresenterManager
import kotlinx.android.synthetic.main.activity_discount_ticket.*

class DiscountTicketActivity : BaseActivity(), IDiscountTicketCallBack {

    private var mDiscountTicketPresenterImpl: DiscountTicketPresenterImpl? = null


    override fun initPresenter() {
        mDiscountTicketPresenterImpl = PresenterManager.getInstance().discountTicketPresenterImpl
        mDiscountTicketPresenterImpl?.registerCallBack(this)
    }

    override fun initEvent() {
        if (CheackUtils.isAppInstalled("com.taobao.taobao")) {
            getTicketTv.text = "复制淘口令并打开淘宝"
        } else {
            getTicketTv.text = "复制淘口令"
        }
    }


    override fun initData() {
        val titleString = intent.getStringExtra("title")
        goodsTitle.text = titleString

    }

    override fun initListener() {
        backIv.setOnClickListener {
            finish()
        }
        getTicketTv.setOnClickListener {
            val cm = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            val clipData = ClipData.newPlainText("huge_code", taokoulin.text)
            cm.setPrimaryClip(clipData)
            if (CheackUtils.isAppInstalled("com.taobao.taobao")){
                val taobaoIntent = Intent()
 /*               taobaoIntent.action = "android.intent.action.MAIN"
                taobaoIntent.addCategory("android.intent.category.LAUNCHER")*/
              val componentName =  ComponentName("com.taobao.taobao","com.taobao.tao.welcome.Welcome")
                taobaoIntent.component = componentName
                startActivity(taobaoIntent)
            }else{
                Toast.makeText(this,"淘口令已经复制",Toast.LENGTH_SHORT).show()
            }

        }

    }

    override fun initView() {

    }

    override fun setLayoutRsId(): Int {
        return R.layout.activity_discount_ticket
    }

    override fun getDataSuccess(body: DiscountTicket, picUrl: String) {
        Glide.with(this).load(picUrl).into(goodsPic)
        if (body.data.tbk_tpwd_create_response!=null){
            taokoulin.setText(body.data.tbk_tpwd_create_response.data.model)
        }else{
            taokoulin.setText("商品已下降了哦")
            getTicketTv.visibility = View.INVISIBLE
        }
    }

    override fun getDataError() {

    }

    override fun noData() {

    }

    override fun loading() {

    }

    override fun onDestroy() {
        Log.i("TAG", "hugea :onDestroy ");
        super.onDestroy()
        if (mDiscountTicketPresenterImpl != null) {
            mDiscountTicketPresenterImpl?.unregisterCallBack()
        }

    }
}